home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / OWNED.PY < prev    next >
Encoding:
Python Source  |  2000-07-10  |  11.6 KB  |  315 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''Support for owned objects
  65.  
  66.  
  67. $Id: Owned.py,v 1.3.18.2 2000/07/10 13:21:23 brian Exp $'''
  68. __version__='$Revision: 1.3.18.2 $'[11:-2]
  69.  
  70. import Globals, urlparse, SpecialUsers, ExtensionClass, string
  71. from AccessControl import getSecurityManager
  72. from Acquisition import aq_get, aq_parent, aq_base
  73.  
  74. UnownableOwner=[]
  75. def ownableFilter(self,
  76.                   aq_get=aq_get,
  77.                   UnownableOwner=UnownableOwner):
  78.     _owner=aq_get(self, '_owner', None, 1)
  79.     return _owner is not UnownableOwner
  80.  
  81. # Marker to use as a getattr default.
  82. _mark=ownableFilter
  83.  
  84. class Owned(ExtensionClass.Base):
  85.  
  86.     __ac_permissions__=(
  87.         ('View management screens',
  88.          ('manage_owner', 'owner_info', 'userCanChangeOwnershipType')),
  89.         ('Take ownership',
  90.          ('manage_takeOwnership','manage_changeOwnershipType'),
  91.          ("Owner",)),
  92.         )
  93.     
  94.     manage_options=({'label':  'Ownership',
  95.                      'action': 'manage_owner',
  96.                      'help':   ('OFSP','Ownership.stx'),
  97.                      'filter': ownableFilter
  98.                      },
  99.                    )
  100.     
  101.     manage_owner=Globals.HTMLFile('owner', globals())
  102.  
  103.     def owner_info(self):
  104.         """Get ownership info for display
  105.         """
  106.         owner=self.getOwner(1)
  107.         if owner is None or owner is UnownableOwner: return owner
  108.         d={'path': string.join(owner[0], '/'), 'id': owner[1],
  109.            'explicit': hasattr(self, '_owner'),
  110.            'userCanChangeOwnershipType':
  111.            getSecurityManager().checkPermission('Take ownership', self)
  112.            }
  113.         return d
  114.     
  115.     getOwner__roles__=()
  116.     def getOwner(self, info=0,
  117.                  aq_get=aq_get, None=None, UnownableOwner=UnownableOwner,
  118.                  ):
  119.         """Get the owner
  120.  
  121.         If a true argument is provided, then only the owner path and id are
  122.         returned. Otherwise, the owner object is returned.
  123.         """
  124.         owner=aq_get(self, '_owner', None, 1)
  125.         if owner is None: return owner
  126.  
  127.         if info: return owner
  128.             
  129.         if owner is UnownableOwner: return None
  130.  
  131.         udb, oid = owner
  132.         root=self.getPhysicalRoot()
  133.         udb=root.unrestrictedTraverse(udb, None)
  134.         if udb is None: return SpecialUsers.nobody
  135.         owner = udb.getUserById(oid, None)
  136.         if owner is None: return SpecialUsers.nobody
  137.         return owner
  138.  
  139.     changeOwnership__roles__=()
  140.     def changeOwnership(self, user,
  141.                         aq_get=aq_get, None=None,
  142.                         ):
  143.         """Change the ownership to the given user, make the
  144.         ownership acquired if possible."""
  145.         new=ownerInfo(user)
  146.         if new is None: return # Special user!
  147.  
  148.         old=aq_get(self, '_owner', None, 1)
  149.  
  150.         if old==new: return
  151.  
  152.         # See if there is an _owner in the instance. If it is a
  153.         # class attribute we leave it alone.
  154.         if self.__dict__.get('_owner', _mark) is not _mark:
  155.             # Hm, maybe we can acquire ownership
  156.             del self._owner
  157.             self.changeOwnership(user)
  158.         else:
  159.             if old is not UnownableOwner:
  160.                 self._owner=new
  161.  
  162.     def userCanTakeOwnership(self):
  163.         security=getSecurityManager()
  164.         user=security.getUser()
  165.         info=ownerInfo(user)
  166.         if info is None: return 0
  167.         owner=self.getOwner(1)
  168.         if owner == info: return 0
  169.         return security.checkPermission('Take ownership', self)
  170.  
  171.     def manage_takeOwnership(self, REQUEST, RESPONSE):
  172.         """Take ownership (responsibility) for an object.
  173.         """
  174.         security=getSecurityManager()
  175.         want_referer=REQUEST['URL1']+'/manage_owner'
  176.         got_referer=("%s://%s%s" %
  177.                      urlparse.urlparse(REQUEST['HTTP_REFERER'])[:3])
  178.         __traceback_info__=want_referer, got_referer
  179.         if (want_referer != got_referer or security.calledByExecutable()):
  180.             raise 'Unauthorized', (
  181.                 'manage_takeOwnership was called from an invalid context'
  182.                 )
  183.         self.changeOwnership(security.getUser())
  184.         RESPONSE.redirect(REQUEST['HTTP_REFERER'])
  185.  
  186.     def manage_changeOwnershipType(self, explicit=1,
  187.                                    RESPONSE=None, REQUEST=None):
  188.         """Change the type (implicit or explicit) of ownership.
  189.         """
  190.         old=getattr(self, '_owner', None)
  191.         if explicit:
  192.             if old is not None: return
  193.             owner=aq_get(self, '_owner', None, 1)
  194.             if owner is not None and owner is not UnownableOwner:
  195.                 self._owner=owner
  196.         else:
  197.             if old is None: return
  198.             new=aq_get(aq_parent(self), '_owner', None, 1)
  199.             if old is new and (
  200.                 self.__dict__.get('_owner', _mark) is not _mark
  201.                 ):
  202.                 del self._owner
  203.  
  204.         if RESPONSE is not None: RESPONSE.redirect(REQUEST['HTTP_REFERER'])
  205.     
  206.     def _deleteOwnershipAfterAdd(self):
  207.  
  208.         # Only delete _owner if it is an instance attribute.
  209.         if self.__dict__.get('_owner', _mark) is not _mark:
  210.             del self._owner
  211.  
  212.         for object in self.objectValues():
  213.             try: s=object._p_changed
  214.             except: s=0
  215.             try: object._deleteOwnershipAfterAdd()
  216.             except: pass
  217.             if s is None: object._p_deactivate()
  218.     
  219.     def manage_fixupOwnershipAfterAdd(self):
  220.  
  221.         # Sigh, get the parent's _owner
  222.         parent=getattr(self, 'aq_parent', None)
  223.         if parent is not None: _owner=aq_get(parent, '_owner', None, 1)
  224.         else: _owner=None
  225.  
  226.         if (_owner is None and
  227.             ((not hasattr(self, 'aq_parent')) or
  228.              (not hasattr(self, 'getPhysicalRoot'))
  229.              )
  230.             ):
  231.             # This is a special case. An object is
  232.             # being added to an object that hasn't
  233.             # been added to the object hierarchy yet.
  234.             # We can delay fixing up the ownership until the
  235.             # object is actually added.
  236.             return None
  237.  
  238.         if _owner is UnownableOwner:
  239.             # We want to acquire Unownable ownership!
  240.             return self._deleteOwnershipAfterAdd()
  241.         else:
  242.             # Otherwise change the ownership
  243.             user=getSecurityManager().getUser()
  244.             if aq_base(user) is SpecialUsers.super:
  245.                 __creatable_by_super__=getattr(self,
  246.                                                '__creatable_by_super__',
  247.                                                None)
  248.                 if (__creatable_by_super__ is None or
  249.                     (not __creatable_by_super__())):                    
  250.                     raise SuperCannotOwn, (
  251.                         "Objects cannot be owned by the superuser")
  252.             self.changeOwnership(user)
  253.  
  254.         # Force all subs to acquire ownership!
  255.         for object in self.objectValues():
  256.             try: s=object._p_changed
  257.             except: s=0
  258.             try: object._deleteOwnershipAfterAdd()
  259.             except: pass
  260.             if s is None: object._p_deactivate()
  261.  
  262.   
  263. Globals.default__class_init__(Owned)
  264.  
  265. class SuperCannotOwn(Exception):
  266.     "The superuser cannot own anything"
  267.  
  268. class EditUnowned(Exception):
  269.     "Can't edit unowned executables"
  270.         
  271.  
  272. def ownerInfo(user,
  273.               getattr=getattr, type=type, st=type(''), None=None):
  274.     uid=user.getId()
  275.     if uid is None: return uid
  276.     db=user.aq_inner.aq_parent
  277.     path=[db.id]
  278.     root=db.getPhysicalRoot()
  279.     while 1:
  280.         db=getattr(db,'aq_inner', None)
  281.         if db is None: break
  282.         db=db.aq_parent
  283.         if db is root: break
  284.         id=db.id
  285.         if type(id) is not st:
  286.             try: id=id()
  287.             except: id=str(id)
  288.         path.append(id)
  289.  
  290.     path.reverse()
  291.  
  292.     return path, uid
  293.  
  294.